#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys

import os
import shutil

############################################################################

src_file_path = r'/rootfs/media/kasim/Data1/data/VideoFaceImage'
dst_file_path = r'/rootfs/media/kasim/Data1/data/VideoFaceImage/headpose'
copy_list_file = r'/rootfs/media/kasim/Data1/data/VideoFaceImage/SelectFace.txt'
# src_file_path = r'/rootfs/media/kasim/Data1/data/VideoFaceImage/headpose'
# dst_file_path = r'/rootfs/media/kasim/Data1/data/VideoFaceImage/headpose_images'
# copy_list_file = r'/rootfs/media/kasim/Data1/data/VideoFaceImage/headpose_select/FilterSelectImage.txt'

try:
    file_count = 0

    if not os.path.exists(dst_file_path):
        os.makedirs(dst_file_path)

    headpose_image_list_path = os.path.join(os.path.dirname(copy_list_file), 'headpose_image_list.txt')
    headpose_image_map_path = os.path.join(os.path.dirname(copy_list_file), 'headpose_image_map.txt')
    list_file = open(headpose_image_list_path, 'w')
    map_file = open(headpose_image_map_path, 'w')
    image_id = 1
    with open(copy_list_file, 'r', encoding='utf-8') as file:
        for line in file.readlines():
            file_name = line.split()[2].strip()
            # file_name = line.split()[0].strip()
            src_path = os.path.join(src_file_path, file_name)
            dst_file_name = '{:06d}.jpg'.format(image_id)
            image_id += 1
            dst_path = os.path.join(dst_file_path, dst_file_name)
            shutil.copy(src_path, dst_path)
            list_file.write(dst_file_name+'\n')
            map_file.write('{} {}\n'.format(file_name, dst_file_name))

            file_count += 1
            if file_count % 100 == 0:
                print('Process Copy File Count:', file_count)
    list_file.close()
    map_file.close()
    os.system('chmod a+wr {}'.format(headpose_image_list_path))
    os.system('chmod a+wr {}'.format(headpose_image_map_path))
    os.system('chmod a+wr {} -Rf'.format(dst_file_path))
    print('Process Copy File Count:', file_count)

except Exception as e:
    print(e)

print('Finish!')

